package com.uenpay.im.invokeitems.chat;

import com.qyx.android.entity.GroupTalkEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;
import com.uenpay.im.serializations.GroupListSerializer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 搜索群聊
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class SearchGroupsInvokeItem extends BaseHttpInvokeItem {
	public class SearchGroupsInvokeItemResult extends HttpInvokeResult {
		public int status;
		public String msg;
		public ArrayList<GroupTalkEntity> groups = new ArrayList<GroupTalkEntity>();
	}

	public SearchGroupsInvokeItemResult getOutput() {
		return (SearchGroupsInvokeItemResult) GetResultObject();
	}

	public SearchGroupsInvokeItem(String keyWord, int page) {
		String url = APIConfiguration.getBaseUrl()
				+ "Chat/Chat/searchChat?searchKey=" + keyWord + "&pageIndex="
				+ page + "&pageSize=12&" + APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		SearchGroupsInvokeItemResult result = new SearchGroupsInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");

			JSONArray array = json.optJSONArray("data");
			if (array == null) {
				return result;
			}

			result.groups = GroupListSerializer.deserializeGroups(array);

			// for (int i = 0, length = array.length(); i < length; i++) {
			// result.groups.add(desGroupTalk(array.optJSONObject(i)));
			// }
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

	// private GroupTalkEntity desGroupTalk(JSONObject json) {
	// GroupTalkEntity mGroupTalk = new GroupTalkEntity();
	// mGroupTalk.group_id = json.optLong("chatid");
	// mGroupTalk.group_name = json.optString("chatname");
	// mGroupTalk.max_count = json.optInt("maxcustnum");
	// mGroupTalk.has_count = json.optInt("custnum");
	// mGroupTalk.join_status = json.optInt("_isjoin");
	// return mGroupTalk;
	// }

}
